//
//  HXLMapViewController.m
//  On The Load
//
//  Created by 晓琳 on 15/11/17.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "HXLMapViewController.h"
#import <MapKit/MapKit.h>
#import "HXLAnnotation.h"
#import "TravelPlan.h"
#import "MBProgressHUD+show.h"
#import "HXLAnnotationView.h"
@interface HXLMapViewController ()<MKMapViewDelegate>
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, assign) CLLocationCoordinate2D preDoc;

@end

@implementation HXLMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:self.mapView];
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    NSInteger num = self.placeArray.count;
    
        if (num > 0) {
            
            TravelPlan *model = [self.placeArray objectAtIndex:0];
            
            CLLocationDegrees longitude = [model.longitude doubleValue];
            CLLocationDegrees latitude = [model.latitude doubleValue];
            self.preDoc = CLLocationCoordinate2DMake(latitude, longitude);
            
        for (int i = 0; i < num; i++) {
            HXLAnnotation *annoView = [[HXLAnnotation alloc] init];
            TravelPlan *model = [self.placeArray objectAtIndex:i];
        
            CLLocationDegrees longitude = [model.longitude doubleValue];
            CLLocationDegrees latitude = [model.latitude doubleValue];
            annoView.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
            annoView.title = model.cityName;
            annoView.icon = @"flag.png";
            
            [self.mapView addAnnotation:annoView];
            
             CLLocationCoordinate2D nextDoc = CLLocationCoordinate2DMake(latitude, longitude);
            
            CLLocationCoordinate2D localArray[2] = {
                self.preDoc,nextDoc
            };
            
            
            self.preDoc = CLLocationCoordinate2DMake(latitude, longitude);

            MKPolyline *line = [MKPolyline polylineWithCoordinates:localArray count:2];
            [self.mapView addOverlay:line];
        }

    }else {
        [MBProgressHUD showInfoWithText:@"请先添加行程"];
    }
    
    
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    HXLAnnotationView *myAnnoView = [HXLAnnotationView myAnnoViewWithMapView:mapView];
    
    return myAnnoView;
//    if ([annotation isKindOfClass:[HXLAnnotation class]]){
//    
//    //定义自己的大头针
//    
//    MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"anno"];
//    if (!annoView) {
//        annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"anno"];
//      
//    
//    }
//    
//
////   annoView.annotation = annotation;
////   annoView.image = ((MKPinAnnotationView *)annotation).;
//        return annoView;
//  
//    }else {
//        return nil;
//    }
}

//定义线的样式
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        
        renderer.strokeColor = [UIColor orangeColor];
        renderer.lineWidth = 1.0;
        return renderer;
    }
    
    return nil;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
